package com.corewillsoft.fireflies.storage.objects;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.ContentValues;
import android.text.TextUtils;

import com.corewillsoft.fireflies.storage.entries.StartGameLevelEntries;

public class StartGameLevel extends BaseGameLevel {

	public StartGameLevel(BaseGameLevel gameLevel) {
		super(gameLevel);
	}

	public StartGameLevel() {
	}

	// values of minimum and maximum energy on firefly initialization
	private final float MINIMUM_INITIAL_ENERGY = 0.5f;
	private final float MAXIMUM_INITIAL_ENERGY = 0.99f;

	@Override
	public List<Float> getFireflyEnergy() {
		List<Float> fireflyEnergyList = new ArrayList<Float>();
		for (int _ : getFireflyIds()) {
			fireflyEnergyList.add(getRandomEnergy());
		}
		return fireflyEnergyList;
	}

	@Override
	public List<Boolean> getFireflyAlive() {
		List<Boolean> fireflyAlive = new ArrayList<Boolean>();
		for (int _ : getFireflyIds()) {
			fireflyAlive.add(true);
		}
		return fireflyAlive;
	}

	@Override
	public List<Boolean> getFireflySaved() {
		List<Boolean> fireflySaved = new ArrayList<Boolean>();
		for (int id : getFireflyIds()) {
			fireflySaved.add(false);
		}
		return fireflySaved;
	}

	// get initial random value of energy (between minimum and maximum values)
	private float getRandomEnergy() {
		Random rand = new Random();
		return (MINIMUM_INITIAL_ENERGY + rand.nextFloat()
				* (MAXIMUM_INITIAL_ENERGY - MINIMUM_INITIAL_ENERGY));
	}

	@Override
	public ContentValues toContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(StartGameLevelEntries.LEVEL, getLevel());
		
		cv.put(StartGameLevelEntries.FIREFLY_IDS, TextUtils.join(",", fireflyIds));
		cv.put(StartGameLevelEntries.FIREFLY_X, TextUtils.join(",", fireflyX));
		cv.put(StartGameLevelEntries.FIREFLY_Y, TextUtils.join(",", fireflyY));

		cv.put(StartGameLevelEntries.PORTAL_IDS, TextUtils.join(",", portalIds));
		cv.put(StartGameLevelEntries.PORTAL_X, TextUtils.join(",", portalX));
		cv.put(StartGameLevelEntries.PORTAL_Y, TextUtils.join(",", portalY));

		cv.put(StartGameLevelEntries.OBSTACLE_IDS, TextUtils.join(",", obstacleIds));
		cv.put(StartGameLevelEntries.OBSTACLE_X, TextUtils.join(",", obstacleX));
		cv.put(StartGameLevelEntries.OBSTACLE_Y, TextUtils.join(",", obstacleY));

		cv.put(StartGameLevelEntries.OBSTACLES_PARAMS, getObstaclesParametersString());

		return cv;
	}

}
